import java.util.Arrays;

public class MyArrayList{
    private int[] elem;
    private int usedSize;

    //顺序表的初始化
    private static final int DEFAULT_SIZE = 2;
    public MyArrayList(){
        this.elem = new int[DEFAULT_SIZE];
    }
    public MyArrayList(int initCapacity){
        this.elem = new int[initCapacity];
    }
    //顺序表的遍历：
    public void display(){
        for(int i = 0;i < this.usedSize;i++){
            System.out.print(this.elem[i]+" ");
        }
        System.out.println();
    }
    //新增元素
    public void add(int data){
        if(isFull()){
            elem = Arrays.copyOf(this.elem,2*this.elem.length);
        }
        this.elem[this.usedSize] = data;
        this.usedSize++;
    }
    public boolean isFull(){
        if(usedSize == elem.length){
            return true;
        }
        return false;
    }

    //在pos位置插入元素
    public void add(int pos,int data){
        if(pos < 0 || pos > usedSize){
            System.out.println(pos+"位置不合法");
        }
        if(isFull()){
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        for(int i = usedSize - 1;i >= pos;i --){
            elem[i +1] = elem[i];
        }
        elem[pos] = data;
        usedSize ++;
    }
    //顺序表的查找
    public int indexof(int toFind){
        for(int i = 0;i <= usedSize - 1;i++){
            if(elem[i] == toFind){
                return i;
            }
        }
        return -1;//在最后一个括号前return!!!
    }

    //顺序表的删除
    public void remove(int toRemove){
        int index = indexof(toRemove);
        if(index == -1){
            System.out.println("没有要删除的元素");
        }
        for(int i = index;i <usedSize;i++){
            elem[i] =elem[i+1];

        }
        usedSize--;
    }





}